<?php
class Admin_ElderlypaymentController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pName = empty($p['name'])?'':Tool_Fnc::safe_string($p['name']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);

        $tSMO = new SeniorModel;
        $tDIMO = new DepartmentinfoModel;
        $tMIMO = new MedicalinsuranceModel;
        $tPIMO = new PaymentinfoModel;
        $tDsMO = new DiscountinfoModel;
        $tElMO = new ElderpaymentModel;
        $tAMO = new AdminModel;
        $tWMO = new WorkerModel;

        $tTime = time();

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID错误'):Tool_Fnc::safe_string($p['sid']);
            $pBasicfee = empty($p['basic_fee'])?Tool_Fnc::ajaxMsg('基本费用不能为空'):Tool_Fnc::safe_string($p['basic_fee']);
            $pPayment = empty($p['payment'])?Tool_Fnc::ajaxMsg('支付方式不能为空'):Tool_Fnc::safe_string($p['payment']);
            $pDiscount = empty($p['discount'])?Tool_Fnc::ajaxMsg('折扣不能为空'):Tool_Fnc::safe_string($p['discount']);
            $pLasttime = empty($p['lasttime'])?Tool_Fnc::ajaxMsg('逾期付费时间不能为空'):Tool_Fnc::safe_string($p['lasttime']);
            $pFee = empty($p['fee'])?Tool_Fnc::ajaxMsg('付费金额不能为空'):Tool_Fnc::safe_string($p['fee']);

            $tData = array(
                'sid' => $pSid,
                'basic_fee' => $pBasicfee,
                'payment' => $pPayment,
                'discount' => $pDiscount,
                'lasttime' => strtotime($pLasttime),
                'fee' => $pFee,
                'is_pay' => 1,
                'handlers' => $_SESSION['admin']['name'],
                'created' => $tTime,
            );
            if(!$tElMO->insert($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            Tool_Fnc::ajaxMsg('操作成功',1);
        }
        
        $tWhere = ' 1 and s.leave_status = 2';
        $tWhere1 = ' 1 ';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and s.leave_status = 2 and s.department = '.$tAData[0]['department'];
                $tWhere1 = ' 1 and is_pay = 1';
            }
        }

        if(!empty($pName)){
            $tWhere .= ' and s.name like \'%'.$pName.'%\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and s.department = '.$pDepartment;
        }

        $tSql = 'select count(s.id) c from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid left join '.$tPIMO->table.' pi on pi.num = mi.payment left join '.$tDsMO->table.' ds on mi.charge_discount = ds.num left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere;
        $tCount = $tSMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tTime = time();

        $tDatas = array();
        if(!empty($tCnt)){
            $tSql = 'select s.id id,s.name name,s.created created,mi.basic_fee basic_fee,pi.introduce payment,pi.num payment_num,ds.introduce discount,ds.num discount_num,di.introduce department from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid left join '.$tPIMO->table.' pi on pi.num = mi.payment left join '.$tDsMO->table.' ds on mi.charge_discount = ds.num left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere.' order by s.id desc limit '.$tPage->limit();
            $tDatas = $tSMO->query($tSql);
        }
        if(!empty($tDatas)){
            foreach($tDatas as $tK=>$tV){
                //查询付费次数
                $tElData = $tElMO->field('count(id) c')->where('sid = '.$tV['id'].' and '.$tWhere1)->fList();
                $tCnt = empty($tElData[0]['c'])?0:$tElData[0]['c'];
                //按照付费方式计算逾期时间
                if($tV['payment_num'] == '1'){#日付
                    $tDatas[$tK]['lasttime'] = strtotime(($tCnt+1).' days',$tV['created']);
                }else if($tV['payment_num'] == '2'){#周付
                    $tDatas[$tK]['lasttime'] = strtotime(($tCnt+1).' week',$tV['created']);
                }else if($tV['payment_num'] == '3'){#月付
                    $tDatas[$tK]['lasttime'] = strtotime(($tCnt+1).' month',$tV['created']);
                }else if($tV['payment_num'] == '4'){#季付
                    $tDatas[$tK]['lasttime'] = strtotime((($tCnt+1)*3).' month',$tV['created']);
                }else if($tV['payment_num'] == '5'){#年付
                    $tDatas[$tK]['lasttime'] = strtotime(($tCnt+1).' year',$tV['created']);
                }
                //查询付费状态
                //$tElRow = $tElMO->field('count(id) c')->where('sid = '.$tV['id'].' and lasttime >= "'.strtotime(date('Y-m-d',$tTime)))->order('id desc')->fRow();
                $tSql = 'select count(id) c from '.$tElMO->table.' where sid = '.$tV['id'].' and lasttime  >= '.strtotime(date('Y-m-d',$tTime)).' order by id desc limit 1';
                $tElRow = $tElMO->query($tSql);
                //查询付费状态
                $tDatas[$tK]['is_pay'] = $tElRow[0]['c'] == 1?1:2;
                //查询付费总额
                $tElDatas = $tElMO->field('sum(fee) fee')->where('sid = '.$tV['id'])->fList();
                $tDatas[$tK]['fee'] = empty($tElDatas[0]['fee'])?0:$tElDatas[0]['fee'];
                //查询付费次数
                $tElCount = $tElMO->field('count(id) c')->where('sid = '.$tV['id'])->fList();
                $tDatas[$tK]['paytimes'] = empty($tElCount[0]['c'])?0:$tElCount[0]['c'];
            }
        }
        //所在部门显示
        $tDIMO = new DepartmentinfoModel;
        $tDIDatas = $tDIMO->field('num,introduce')->fList();

        $this->assign('pName',$pName);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
    }
    public function detailAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('ID不能为空'):Tool_Fnc::safe_string($p['id']);

        $tMO = new ElderpaymentModel;
        $tSMO = new SeniorModel;
        $tSData = $tSMO->field('name')->where('id = '.$pId)->fRow();
        
        $tWhere = ' 1 ';

        $tCount = $tMO->field('count(id) c')->where('sid = '.$pId.' and '.$tWhere)->fList();
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
            $tDatas = $tMO->field('id,created,fee,handlers')->where('sid = '.$pId.' and '.$tWhere)->order('id desc')->limit($tPage->limit())->fList();
        }
        
        $this->assign('tName',$tSData['name']);
        $this->assign('tDatas',$tDatas);
        $this->assign('tShow',$tPage->show());
    }

    //时间差函数
    public function timeDiff( $begin_time, $end_time ){
        if ( $begin_time < $end_time ) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime - $starttime;
        $days = intval( $timediff / 86400 );
        $remain = $timediff % 86400;
        $hours = intval( $remain / 3600 );
        $remain = $remain % 3600;
        $mins = intval( $remain / 60 );
        $secs = $remain % 60;
        $res = array( "day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
        return $res;
    }
}
?>
